package com.miyu.writenum;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends Activity {
    private long exitTime = 0;
    static boolean isPlay = true;
    MediaPlayer mediaPlayer;
    //    Button music_btn;
    @BindView(R.id.btn_music)
    Button music_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // music_btn = findViewById(R.id.btn_music);
        ButterKnife.bind(this);
        PlayMusic();

    }

    private void PlayMusic() {
        mediaPlayer = MediaPlayer.create(this, R.raw.main_music);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
    }

    public void OnPlay(View v) {

        startActivity(new Intent(MainActivity.this, SelectActivity.class));
        // finish();
    }

    public void OnAbout(View v) {
        startActivity(new Intent(MainActivity.this, AboutActivity.class));
        // finish();

    }

    public void OnMusic(View v) {
        if (isPlay) {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                ;
                music_btn.setBackgroundResource(R.drawable.btn_music2);
                isPlay = false;
            }

        } else {
            PlayMusic();
            music_btn.setBackgroundResource(R.drawable.btn_music1);
            isPlay = true;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (isPlay) {
            PlayMusic();
        }
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            //彻底关闭整个APP
            int currentVersion = android.os.Build.VERSION.SDK_INT;
            if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);
                System.exit(0);

            }
        }
    }
}
